package com.ljx.consumer.listeners;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-06-27  16:18
 */
@Slf4j
@Component
public class MqListener {

    // 简单消息
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg) throws Exception {
        log.info("收到消息：{}", msg);
        throw new Exception("故意的");
    }

    // 工作模式
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        log.info("消费者1收到消息：{}", msg);
        Thread.sleep(20);
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        log.info("消费者2收到消息：....{}", msg);
        Thread.sleep(200);
    }

    // fanout模式
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) throws InterruptedException {
        log.info("消费者1收到消息：{}", msg);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) throws InterruptedException {
        log.info("消费者2收到消息：....{}", msg);
    }


    // direct模式
    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue1(String msg) throws InterruptedException {
        log.info("消费者1收到消息：{}", msg);
    }

    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String msg) throws InterruptedException {
        log.info("消费者2收到消息：....{}", msg);
    }

    // 注解绑定
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue3", durable = "true"),
            exchange = @Exchange(name = "test.direct2", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void listenDirectQueue3(String msg) throws InterruptedException {
        log.info("消费者2收到消息：....{}", msg);
    }

    // topic模式
    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String msg) throws InterruptedException {
        log.info("消费者1收到消息：{}", msg);
    }

    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String msg) throws InterruptedException {
        log.info("消费者2收到消息：....{}", msg);
    }

    // 消息序列化
    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String, String> msg) {
        log.info("消费者收到消息：....{}", msg);
    }

    // Lazy Out
    @RabbitListener(queuesToDeclare = @Queue(
            name = "lazy.queue",
            durable = "true",
            arguments = @Argument(name = "x-queue-mode", value = "lazy")
    ))
    public void listenLazyQueue(String msg) {
        log.info("消费者收到消息：....{}", msg);
    }

    // 死信队列
    @RabbitListener(queues = "dlx.queue")
    public void listenDlxQueue(String msg) {
        log.info("消费者收到消息：....{}", msg);
    }

    // 延迟消息（插件）
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue", durable = "true"),
            exchange = @Exchange(name = "delay.direct", delayed = "true"),
            key = {"hi", "hello"}
    ))
    public void listenDelayQueue(String msg) throws InterruptedException {
        log.info("消费者收到消息：....{}", msg);
    }
}